﻿package cn.antscript.airCMS 
{
	import cn.antscript.airCMS.data.Config;
	import cn.antscript.airCMS.data.XMLData;
	import cn.antscript.airCMS.display.component.AbstractItemContainer;
	import cn.antscript.airCMS.display.component.AlertPanel;
	import cn.antscript.airCMS.display.component.btnbase.IcoBitmapdatas;
	import cn.antscript.airCMS.display.component.UserManagerPanel;
	import cn.antscript.airCMS.display.component.MYSQLManager;
	import cn.antscript.airCMS.display.component.WaitBar;
	import cn.antscript.airCMS.display.ImgViewer;
	import cn.antscript.airCMS.display.LoginPanel;
	import cn.antscript.airCMS.display.VideoViewer;
	import com.adobe.crypto.MD5;
	import com.bit101.components.PushButton;
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.Capabilities;
	
	/**
	 * ...
	 * @author 
	 */
	public class Main extends Sprite
	{
		public static const WinWidth:int = 800;
		public static const WinHeight:int = 600;
		
		private var _loginPanel:LoginPanel;
		private var _toolPanel:AbstractItemContainer;
		private var _changeBtn:PushButton;
		private var _quit:PushButton;
		
		
		public function Main() 
		{
			addEventListener(Event.ADDED_TO_STAGE, onAdd);
			
		}
		
		private function onAdd(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAdd);
			
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			//stage.nativeWindow.addEventListener(Event.DEACTIVATE, onDock);
			
			
			
			
			_loginPanel = new LoginPanel();
			addChild(_loginPanel);
			_loginPanel.addEventListener(Event.COMPLETE, onLoginCom);
			
			
			if (MYSQLManager.dbFile.exists) {
				MYSQLManager.getInstance().loadData()
			}
			else {
				MYSQLManager.getInstance().show(stage);
			}
			
			
		}
		
		private function onClosing(e:Event=null):void 
		{
			e.preventDefault();
			
			ImgViewer.getInstance().closeWin();
			VideoViewer.getInstance().closeWin();
			
			var loader:URLLoader = new URLLoader();
			loader.load(new URLRequest(Config.UPDATE_URL));
			loader.addEventListener(Event.COMPLETE, onCom);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			WaitBar.getInstance().show(this.stage);
		}
		
		private function onCom(e:Event):void 
		{
			WaitBar.getInstance().hide();
			
			stage.nativeWindow.close();
		}		
		private function onErr(e:IOErrorEvent):void 
		{
			WaitBar.getInstance().hide();
			
			AlertPanel.getInstance().show("保存错误。",this.stage);
			
			stage.nativeWindow.close();
		}
		
		private function onDock(e:Event):void 
		{
			stage.nativeWindow.visible = false;
			NativeApplication.nativeApplication.icon.bitmaps = [IcoBitmapdatas.Music];
			NativeApplication.nativeApplication.icon.addEventListener(MouseEvent.CLICK, onUndock);			
		}
		private  function onUndock(e:MouseEvent):void {
			stage.nativeWindow.visible = true;
			stage.nativeWindow.activate();
			stage.nativeWindow.orderToFront();
			NativeApplication.nativeApplication .icon.bitmaps = [];
			NativeApplication.nativeApplication.icon.removeEventListener(MouseEvent.CLICK, onUndock);
		}
		
		private function onLoginCom(e:Event):void 
		{
			_loginPanel.addEventListener(Event.COMPLETE, onLoginCom);
			removeChild(_loginPanel);			
			
			
			stage.nativeWindow.width = WinWidth;
			stage.nativeWindow.height = WinHeight;
			stage.nativeWindow.x = (Capabilities.screenResolutionX - WinWidth) / 2;
			stage.nativeWindow.y = (Capabilities.screenResolutionY - WinHeight) / 2;
			
			WaitBar.getInstance().show(this.stage);
			XMLData.getInstance().loadXML();
			XMLData.getInstance().addEventListener(Event.COMPLETE, onLoadCom);
			
			
			
		}
		
		private function onLoadCom(e:Event):void 
		{
			XMLData.getInstance().removeEventListener(Event.COMPLETE, onLoadCom);
			
			WaitBar.getInstance().hide();
			
			_toolPanel = new AbstractItemContainer();
			_toolPanel.x = 40;
			_toolPanel.y = 100;
			addChild(_toolPanel);
			
			_toolPanel.fillData(XMLData.getInstance());
			
			stage.nativeWindow.addEventListener(Event.CLOSING, onClosing);
			
			_changeBtn = new PushButton(this, stage.stageWidth-204, 2, "修改密码", changePassword);
			_quit = new PushButton(this, stage.stageWidth-102, 2, "退   出", onClosing);
		}
		
		private function changePassword(e:MouseEvent):void
		{
			UserManagerPanel.getInstance().show(stage);
		}
		
	}

}